#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：LittlePython 
@File    ：监听.py
@Author  ：LinZe
@Date    ：2025/2/25 11:30 
'''
import socket

# @description: 开启一个服务端监听指定IP和端口
def start_server(ip, port):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
        server_socket.bind((ip, port))
        server_socket.listen()
        print(f"Listening on {ip}:{port}")
        while True:
            conn, addr = server_socket.accept()
            with conn:
                print(f"Connected by {addr}")
                while True:
                    data = conn.recv(1024)
                    if not data:
                        break
                    print(f"Received message: {data.decode('utf-8')}")


ip = "0.0.0.0"  # 监听所有可用的网络接口
port = 12345   # 替换为您希望监听的端口
start_server(ip, port)